package com.example.test.util;

import org.springframework.web.multipart.MultipartFile;

import java.io.*;

/**
 * @Author 林文峰
 * @Date 2020/6/19 0019 18:11
 */
public class MultipartToFileDemo {
	
	public static File toFile(MultipartFile file){
		File toFile = null;
		if (file.equals("") || file.getSize() <= 0){
			file = null;
		}
		InputStream ins = null;
		OutputStream ous = null;
		try {
			ins = file.getInputStream();
			toFile = new File(file.getOriginalFilename());
			ous = new FileOutputStream(toFile);
			int temp = 0;
			byte[] bytes = new byte[file.getBytes().length];
			while ((temp = ins.read(bytes,0,file.getBytes().length)) != -1){
				ous.write(bytes,0,file.getBytes().length);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				ous.close();
				ins.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return toFile;
	}
}
